﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace X3DLib
{
    public class XmlDocOperate
    {
        // 获得Xml文档
        public static XmlDocument getXmlDoc(Stream instream)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(instream);
            return doc;
        }
        public static XmlDocument getXmlDoc(string filepath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(filepath);
            return doc;
        }
        public static XmlDocument getXmlDoc(TextReader treader)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(treader);
            return doc;
        }
        public static XmlDocument getXmlDoc(XmlReader xreader)
        {
            XmlDocument doc = new XmlDocument();        //远程时出现Web Exception
            try
            {
                doc.Load(xreader);
                return doc;
            }
            catch (Exception ex)
            {
                return null;
            }
            
        }
        //将xml格式的字符串加载为XmlDocument
        public static XmlDocument loadXmlByStr(string xml)//远程登录时，出现Web Exception
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.LoadXml(xml);
                return doc;
            }
            catch (Exception ex)
            {
                return null;
            }
            
        }
        //保存xml文档
        public static void saveXmlDoc(XmlDocument doc, string filepath)
        {
            doc.Save(filepath);
        }

    }
}
